Skip to content

db(schema): add check constraints for positive monetary amounts and v…#383

Merged
kilodesodiq-arch merged 1 commit into
ChainForgee:mainfrom
fikrah-Tech:db/schema-check-constraints
Jul 19, 2026
Merged

db(schema): add check constraints for positive monetary amounts and v…#383
kilodesodiq-arch merged 1 commit into
ChainForgee:mainfrom
fikrah-Tech:db/schema-check-constraints

Conversation

@yunus-dev-codecrafter

Copy link
Copy Markdown
Contributor

closes #263
…alid key rotation windows

Summary

Adds DB-level CHECK constraints on three monetary/temporal fields that previously lacked schema-level guards:

  • Claim.amount > 0
  • BalanceLedger.amount != 0
  • ApiKey.revokedAt >= createdAt (passes when revokedAt IS NULL)

Existing application logic already prevents violations, but no DB-level guarantee existed.

Closes #263

Testing

  • Created a hand-written Prisma migration at app/backend/prisma/migrations/20260718000000_add_check_constraints/
  • Applied baseline + migration against an in-memory SQLite database using node:sqlite
  • Verified all 6 test cases:
cd app/backend

# Schema validation (DATABASE_URL must be a valid Postgres URL):
DATABASE_URL="postgresql://localhost:5432/test" npx prisma validate

# Apply migration on local SQLite dev db:
npx prisma migrate deploy

# Or generate a new migration via Prisma Migrate:
npx prisma migrate dev --name add_check_constraints

Test matrix (all pass):

Test case Expected Result
Insert Claim with amount = -50 Rejected (CHECK)
Insert BalanceLedger with amount = 0 Rejected (CHECK)
Insert ApiKey with revokedAt < createdAt Rejected (CHECK)
Insert ApiKey with revokedAt = createdAt Allowed
Insert ApiKey with revokedAt > createdAt Allowed
Insert ApiKey with revokedAt IS NULL Allowed

Screenshots

N/A

Checklist

  • Tests added or updated for new/changed behavior
  • Existing tests pass
  • No secrets, keys, or seed phrases committed
  • Follows the coding conventions in the relevant service README
  • PR is focused on a single concern

@kilodesodiq-arch kilodesodiq-arch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kilodesodiq-arch
kilodesodiq-arch merged commit 6dcfb3d into ChainForgee:main Jul 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add row-level constraints on critical invariants (e.g. Claim.amount >= 0)

2 participants